package com.xy.farm.activity.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xy.farm.R;
import com.xy.farm.activity.adapter.PageAdapter;
import com.xy.farm.activity.base.BaseFragment;
import com.xy.farm.activity.deal.BrandStoryActivity;
import com.xy.farm.activity.deal.FarmStrategyActivity;
import com.xy.farm.activity.deal.SearchActivity;
import com.xy.farm.activity.deal.SelectCityActivity;
import com.xy.farm.activity.deal.ShelvesActivity;
import com.xy.farm.activity.deal.adapter.DealGoodsAdapter;
import com.xy.farm.activity.fragment.adapter.DealAllAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 1174607250 on 2018/8/6.
 */

public class DealFragment extends BaseFragment
{
    private RecyclerView recyclerView;
    private DealGoodsAdapter goodsAdapter;

    private ViewPager viewPager;
    private TabLayout tabLayout;
    private PageAdapter adapter;
    private RecyclerView recyclerView1;
    private RecyclerView recyclerView2;
    private RecyclerView recyclerView3;
    private RecyclerView recyclerView4;
    private RecyclerView recyclerView5;
    private List<View> views = new ArrayList<>();
    private List<String> strings = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_deal, container, false);
        view.findViewById(R.id.btn_search).setOnClickListener(this);
        view.findViewById(R.id.btn_select_city).setOnClickListener(this);
        view.findViewById(R.id.btn_brand_story).setOnClickListener(this);
        view.findViewById(R.id.btn_farm_strategy).setOnClickListener(this);
        view.findViewById(R.id.btn_tyrant).setOnClickListener(this);
        recyclerView = view.findViewById(R.id.recyclerView);
        tabLayout = view.findViewById(R.id.tabLayout);
        viewPager = view.findViewById(R.id.viewPager);
        return view;
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        recyclerView.setHasFixedSize(true);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity())
        {
            @Override
            public boolean canScrollVertically()
            {
                return false;
            }
        };
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter((goodsAdapter = new DealGoodsAdapter()));
        strings.add("全部");
        strings.add("关注");
        strings.add("水果");
        strings.add("蔬菜");
        strings.add("牲畜");
        views.add(LayoutInflater.from(getActivity()).inflate(R.layout.layout_deal_recycler_view, null));
        views.add(LayoutInflater.from(getActivity()).inflate(R.layout.layout_deal_recycler_view, null));
        views.add(LayoutInflater.from(getActivity()).inflate(R.layout.layout_deal_recycler_view, null));
        views.add(LayoutInflater.from(getActivity()).inflate(R.layout.layout_deal_recycler_view, null));
        views.add(LayoutInflater.from(getActivity()).inflate(R.layout.layout_deal_recycler_view, null));
        viewPager.setAdapter(( adapter = new PageAdapter(views, strings)));
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabsFromPagerAdapter(adapter);
        recyclerView1 = views.get(0).findViewById(R.id.recyclerView);
        recyclerView2 = views.get(1).findViewById(R.id.recyclerView);
        recyclerView3 = views.get(2).findViewById(R.id.recyclerView);
        recyclerView4 = views.get(3).findViewById(R.id.recyclerView);
        recyclerView5 = views.get(4).findViewById(R.id.recyclerView);
        recyclerView1.setHasFixedSize(true);
        recyclerView1.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        recyclerView1.setAdapter(new DealAllAdapter());
    }

    @Override
    public void onClick(View v)
    {
        super.onClick(v);
        Intent intent;
        switch (v.getId())
        {
            case R.id.btn_search:
                intent = new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_select_city:
                intent = new Intent(getActivity(), SelectCityActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_brand_story:
                intent = new Intent(getActivity(), BrandStoryActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_farm_strategy:
                intent = new Intent(getActivity(), FarmStrategyActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_tyrant:
                showShortToast("该功能暂未开放");
                break;
        }
    }
}
